package com.desmart.renjiao.attendance;

import android.util.Log;

import com.desmart.renjiao.App;
import com.desmart.renjiao.bean.CheckMonthResult;
import com.desmart.renjiao.bean.CheckRecord;
import com.desmart.renjiao.bean.ChekDayResult;
import com.desmart.renjiao.bean.LoginBean;
import com.desmart.renjiao.bean.WorkDay;
import com.desmart.renjiao.xmlparase.ResultXmlUtils;
import com.google.gson.Gson;
import com.lemon.library.constants.PreferencesConstants;
import com.lemon.library.net.rx.RxObservableListener;
import com.lemon.library.utils.PreferencesUtils;
import com.lemon.library.utils.ToastUtils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class AttendanceRecordP extends AttendanceRecord.AttendanceRecordP{


    @Override
    void checkServiceByMonth(int year, int month) {

        String sapno = null;
        String employeecardid = null;

        String loginInfoStr = PreferencesUtils.getString(PreferencesConstants.login_info);
        if (loginInfoStr != null){
            LoginBean loginBean = new Gson().fromJson(loginInfoStr, LoginBean.class);
            sapno = loginBean.getSapno();
            employeecardid = loginBean.getEmployeecardid();
        }

        String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.destram.cn.com/\">" +
                "<soapenv:Header/><soapenv:Body><ser:GetCheckByMonth><arg0>" +
                sapno +
                "</arg0><arg1>" +
                employeecardid +
                "</arg1><arg2>" +
                year + "-" + month +
                "</arg2><arg3>5</arg3></ser:GetCheckByMonth>" +
                "</soapenv:Body>" +
                "</soapenv:Envelope>";

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), xml);
        getRxManager().addObserver(mModel.checkServiceByMonth(body), new RxObservableListener<ResponseBody>(mView) {
            @Override
            public void onNext(ResponseBody responseBody) {
                super.onNext(responseBody);
                String ss = null;

                try {
                    ss = responseBody.string();
                    InputStream inputStream = new ByteArrayInputStream(ss.getBytes("UTF-8"));
                    String result = ResultXmlUtils.readXML(inputStream);
                    CheckMonthResult checkMonthResult = new Gson().fromJson(result, CheckMonthResult.class);
                    if ("200".equals(checkMonthResult.getCode())){
                        List<WorkDay> errorDayList = checkMonthResult.getErrorDayList();
                        mView.onSuccessGetMonthErrorList(errorDayList,year,month);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    @Override
    void checkServiceByDay(int year, int month, int day) {

        String sapno = null;
        String employeecardid = null;

        String loginInfoStr = PreferencesUtils.getString(PreferencesConstants.login_info);
        if (loginInfoStr != null){
            LoginBean loginBean = new Gson().fromJson(loginInfoStr, LoginBean.class);
            sapno = loginBean.getSapno();
            employeecardid = loginBean.getEmployeecardid();
        }

        String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.destram.cn.com/\">" +
                "<soapenv:Header/><soapenv:Body><ser:GetCheckByDay><arg0>" +
                sapno +
                "</arg0><arg1>" +
                employeecardid +
                "</arg1><arg2>" +
                year + "-" + month + "-" +day +
                "</arg2></ser:GetCheckByDay>" +
                "</soapenv:Body>" +
                "</soapenv:Envelope>";

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), xml);
        getRxManager().addObserver(mModel.checkServiceByDay(body), new RxObservableListener<ResponseBody>(mView) {
            @Override
            public void onNext(ResponseBody responseBody) {
                super.onNext(responseBody);
                String ss = null;

                try {
                    ss = responseBody.string();
                    InputStream inputStream = new ByteArrayInputStream(ss.getBytes("UTF-8"));
                    String result = ResultXmlUtils.readXML(inputStream);
                    Log.e("Hqq",result);
                    ChekDayResult chekDayResult = new Gson().fromJson(result, ChekDayResult.class);
                    if ("200".equals(chekDayResult.getCode())){
                        List<CheckRecord> recordsList = chekDayResult.getRecordsList();
                        List<CheckRecord> errorList = chekDayResult.getErrorList();
                        mView.onSuccessGetDayList(recordsList,errorList);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });


    }


}
